package cn.ibizlab.odoo.core.odoo_uom.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iuom_uom;
import cn.ibizlab.odoo.core.odoo_uom.domain.Uom_uom;
import cn.ibizlab.odoo.core.odoo_uom.filter.Uom_uomSearchContext;
import cn.ibizlab.odoo.core.odoo_uom.service.IUom_uomService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[产品计量单位] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Uom_uomServiceImpl implements IUom_uomService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iuom_uomClientService uom_uomClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iuom_uom clientModel = uom_uomClientService.createModel();
        clientModel.setId(id);
        uom_uomClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Uom_uom uom_uom) {
        boolean bOk = false ;
        Iuom_uom clientModel = this.convert2Model(uom_uom,null);
        uom_uomClientService.update(clientModel) ;
        this.convert2Domain(clientModel,uom_uom) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Uom_uom> uom_uoms, int batchSize) {

                        //uom_uomClientService.createBatch(uom_uoms,batchSize) ;

    }

    @Override
    public Uom_uom get(Integer id) {
        Iuom_uom clientModel = uom_uomClientService.createModel();
        clientModel.setId(id);
        uom_uomClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Uom_uom get(Uom_uom uom_uom) {
        Uom_uom retuom_uom = this.get(uom_uom.getId()) ;
    	BeanCopier copier=BeanCopier.create(retuom_uom.getClass(), uom_uom.getClass(), false);
        copier.copy(retuom_uom, uom_uom, null);
        return uom_uom ;
    }

    @Override
    public void updateBatch(List<Uom_uom> uom_uoms, int batchSize) {

        //uom_uomClientService.updateBatch(uom_uoms,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //uom_uomClientService.removeBatch(uom_uoms) ;

    }

    @Override
    public boolean create(Uom_uom uom_uom) {
        boolean bOk = false ;
        Iuom_uom clientModel = this.convert2Model(uom_uom,null);
        uom_uomClientService.create(clientModel) ;
        this.convert2Domain(clientModel,uom_uom);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Uom_uom> searchDefault(Uom_uomSearchContext context) {
        List<Uom_uom> list = new ArrayList<Uom_uom>();
        Page<cn.ibizlab.odoo.core.client.model.Iuom_uom> clientModelList = uom_uomClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iuom_uom clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Uom_uom>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Uom_uom> selectByOdoo_create(Integer id){
        Uom_uomSearchContext context = new Uom_uomSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Uom_uom> list = new ArrayList<Uom_uom>();
        Page<cn.ibizlab.odoo.core.client.model.Iuom_uom> clientModelList = uom_uomClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iuom_uom clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Uom_uom>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Uom_uom> selectByOdoo_write(Integer id){
        Uom_uomSearchContext context = new Uom_uomSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Uom_uom> list = new ArrayList<Uom_uom>();
        Page<cn.ibizlab.odoo.core.client.model.Iuom_uom> clientModelList = uom_uomClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iuom_uom clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Uom_uom>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位 类别查询
     */
    @Override
    public Page<Uom_uom> selectByOdoo_category(Integer id){
        Uom_uomSearchContext context = new Uom_uomSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Uom_uom> list = new ArrayList<Uom_uom>();
        Page<cn.ibizlab.odoo.core.client.model.Iuom_uom> clientModelList = uom_uomClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iuom_uom clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Uom_uom>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iuom_uom convert2Model(Uom_uom domain , Iuom_uom model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = uom_uomClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getFactor_invDirtyFlag())
            model.setFactor_inv(domain.getFactor_inv());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getRoundingDirtyFlag())
            model.setRounding(domain.getRounding());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getFactorDirtyFlag())
            model.setFactor(domain.getFactor());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getUom_typeDirtyFlag())
            model.setUom_type(domain.getUom_type());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCategory_id_textDirtyFlag())
            model.setCategory_id_text(domain.getCategory_id_text());
         if(domain.getIs_pos_groupableDirtyFlag())
            model.setIs_pos_groupable(domain.getIs_pos_groupable());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getMeasure_typeDirtyFlag())
            model.setMeasure_type(domain.getMeasure_type());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCategory_idDirtyFlag())
            model.setCategory_id(domain.getCategory_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Uom_uom convert2Domain( Iuom_uom model ,Uom_uom domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Uom_uom();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getFactor_invDirtyFlag())
            domain.setFactor_inv(model.getFactor_inv());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getRoundingDirtyFlag())
            domain.setRounding(model.getRounding());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getFactorDirtyFlag())
            domain.setFactor(model.getFactor());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getUom_typeDirtyFlag())
            domain.setUom_type(model.getUom_type());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCategory_id_textDirtyFlag())
            domain.setCategory_id_text(model.getCategory_id_text());
        if(model.getIs_pos_groupableDirtyFlag())
            domain.setIs_pos_groupable(model.getIs_pos_groupable());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getMeasure_typeDirtyFlag())
            domain.setMeasure_type(model.getMeasure_type());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCategory_idDirtyFlag())
            domain.setCategory_id(model.getCategory_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
